package com.yhyvr.api.service.store;

import com.google.common.collect.Lists;
import com.lz.mybatis.jdbc.auto.dao.BoCommentMapper;
import com.lz.mybatis.jdbc.auto.dao.BoProxyMapper;
import com.lz.mybatis.jdbc.auto.dao.BoProxyOrderGoodsMapper;
import com.lz.mybatis.jdbc.auto.model.*;
import com.lz.tool.ListUtils;
import com.lz.tool.UUIDTool;
import com.lz.tool.date.DateStyle;
import com.lz.tool.date.DateUtil;
import com.yhyvr.api.handle.AuthAnnotation;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.store.comment.ApiCommentAddReq;
import com.yhyvr.pojo.req.api.store.comment.ApiCommentsAddReq;
import com.yhyvr.pojo.req.api.store.comment.ApiGoodsCommentReq;
import com.yhyvr.pojo.req.api.store.order.ApiOrderInfoReq;
import com.yhyvr.pojo.resp.api.ApiRespBody;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.ApiResultCode;
import com.yhyvr.pojo.resp.api.store.comment.ApiCommentGoodsResp;
import com.yhyvr.pojo.resp.api.store.comment.ApiCommentResp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 评论
 * Created by Administrator on 2017/6/22.
 */

@Service
public class ApiCommentService {

    @Resource
    private BoProxyOrderGoodsMapper boProxyOrderGoodsMapper;
    @Resource
    private BoCommentMapper boCommentMapper;

    @Resource
    private BoProxyMapper boProxyMapper;
    /**
     * 添加评论
     * @param apiCommentsAddReqApiReqData
     * @return
     */
    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiRespBody> addComment(ApiReqData<ApiCommentsAddReq> apiCommentsAddReqApiReqData){
        ApiCommentsAddReq body = apiCommentsAddReqApiReqData.getBody();
        List<ApiCommentAddReq> comments = body.getComments();
        BoCommentExample boCommentExample = new BoCommentExample();
        boCommentExample.createCriteria().andProxyIdEqualTo(apiCommentsAddReqApiReqData.getCookieUserId()).andOrderIdEqualTo(body.getOrderId());
        if(boCommentMapper.countByExample(boCommentExample)>0){
            return ApiRespData.buildFail(ApiResultCode.VERIFY_CODE_ERROR);
        }
        for (ApiCommentAddReq comment : comments) {
            BoComment boComment = new BoComment();
            boComment.setContent(comment.getComment());
            boComment.setCreateTime(new Date());
            boComment.setGradeClean(comment.getGradeClean());
            boComment.setGradeFresh(comment.getGradeFresh());
            boComment.setGradePack(comment.getGradePack());
            boComment.setGradeTaste(comment.getGradeTaste());
            boComment.setId(UUIDTool.getUUID());
            boComment.setOrderId(body.getOrderId());
            boComment.setGoodsId(comment.getGoodsId());
            boComment.setImages(comment.getImages());
            boComment.setProxyId(apiCommentsAddReqApiReqData.getCookieUserId());
            boCommentMapper.insertSelective(boComment);
        }
        return ApiRespData.buildSucc();

    }

    @AuthAnnotation(iLogin = true)
    public ApiRespData<List<ApiCommentGoodsResp>> commentGoods(ApiReqData<ApiOrderInfoReq> apiCommentsAddReqApiReqData){
        List<ApiCommentGoodsResp> list = Lists.newArrayList();
        BoProxyOrderGoodsExample boProxyOrderGoodsExample = new BoProxyOrderGoodsExample();
        boProxyOrderGoodsExample.createCriteria().andOrderIdEqualTo(apiCommentsAddReqApiReqData.getBody().getOrderId());
        List<BoProxyOrderGoods> boProxyOrderGoods = boProxyOrderGoodsMapper.selectByExample(boProxyOrderGoodsExample);
        for (BoProxyOrderGoods boProxyOrderGood : boProxyOrderGoods) {
            ApiCommentGoodsResp apiCommentGoodsResp = new ApiCommentGoodsResp();
            apiCommentGoodsResp.setGoodsId(boProxyOrderGood.getGoodsId());
            apiCommentGoodsResp.setGoodsImage(boProxyOrderGood.getOrderGoodsImage());
            apiCommentGoodsResp.setGoodsTitle(boProxyOrderGood.getOrderGoodsTitle());
            list.add(apiCommentGoodsResp);
        }

        return ApiRespData.buildSucc(list);

    }

    public ApiRespData<List<ApiCommentResp>> commentList(ApiReqData<ApiGoodsCommentReq> apiCommentsAddReqApiReqData){
        List<ApiCommentResp> list = Lists.newArrayList();
        BoCommentExample boCommentExample = new BoCommentExample();
        boCommentExample.createCriteria().andGoodsIdEqualTo(apiCommentsAddReqApiReqData.getBody().getGoodsId());
        List<BoComment> boComments = boCommentMapper.selectByExample(boCommentExample);
        if(ListUtils.isNotBlank(boComments)){
            for (BoComment boComment : boComments) {
                ApiCommentResp apiCommentResp = new ApiCommentResp();
                apiCommentResp.setContent(boComment.getContent());
                apiCommentResp.setCreateTime(DateUtil.DateToString(boComment.getCreateTime(), DateStyle.MM_DD_HH_MM_CN));
                apiCommentResp.setImages(boComment.getImages());
                BoProxy boProxy = boProxyMapper.selectByPrimaryKey(boComment.getProxyId());
                apiCommentResp.setProxyName(boProxy.getBoProxyNickName());
                list.add(apiCommentResp);
            }
        }
        return ApiRespData.buildSucc(list);

    }
}
